$(document).ready(function() {  
    $('#select_ordre').change(function (){
        getSuperFamilles($('#select_ordre :selected').val());
    })
});

function getSuperFamilles(order_id){
    var data = "nav=superFamille&order="+ order_id;
    var listeSuperFamille = ajaxSend(data);
    
    for(var i=0; i<listeSuperFamille.length; i++){
        var id=  get_Id_Unique(listeSuperFamille[i]['id']);
        
        var div = $("<div id='div_Superfamille_" + id + "' class='div_Superfamille' style='display:block'></div>");
        var div_tete = $("<div class='div_entete'></div>");
        var span_info = $("<span class='div_title'>Superfamille : </span>");
        var span = $("<span>" + listeSuperFamille[i]['nom'] + "</span>");
        var ajouter ="<a class='lien_ajout' onclick=\"ajouter('famille', "+listeSuperFamille[i]['id']+");\" ><span>Ajouter famille(s)</span></a>"
        var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + listeSuperFamille[i]['id'] + ", '" + listeSuperFamille[i]['nom'] + "');\" />");
        var fleche = $("<img src='../../content/images/fleche_bas.png' id='fl_"+id+"' class='img_fleche img' onclick=\"getFamilleList('" + id + "');\" />");
        var corps = $("<div class='div_corps' style='display:none;'></div>");
        div_tete.append(fleche);
        div_tete.append(span_info);
        div_tete.append(span);
        div_tete.append(supp);
        div_tete.append(ajouter);
        div.append(div_tete); 
        div.append(corps);
        $("#contenu_taxonomie").append(div);
    }
}

function getFamilleList(sup_fam_id){
    if(verification("div_Superfamille_"+sup_fam_id)){
        var id_sp = retreive_Id(sup_fam_id);
        var data = "nav=famille&supFam="+ id_sp;
        var listeFamille = ajaxSend(data);
        
        for(var i=0; i<listeFamille.length; i++){
            var id= get_Id_Unique(listeFamille[i]['id']);
            var div = $("<div id='div_Famille_" + id + "' class='div_Famille' style='display:block'></div>");
            var div_tete = $("<div class='div_entete'></div>");
            var span = $("<span>" + listeFamille[i]['nom'] + "</span>");
            var span_info = $("<span class='div_title'>Famille : </span>");
            var ajouter ="<a class='lien_ajout' onclick=\"ajouter('sousfam', "+listeFamille[i]['id']+");\" ><span>Ajouter sous-famille(s)</span></a>"
            var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + listeFamille[i]['id'] + ", '" + listeFamille[i]['nom'] + "');\" />");
            var fleche = $("<img src='../../content/images/fleche_bas.png' id='fl_"+id+"' class='img_fleche img' onclick=\"getSousFamilleList('"+sup_fam_id+"', '" + id + "');\" />");
            var corps = $("<div class='div_corps' style='display:none;'></div>");
            div_tete.append(fleche);
            div_tete.append(span);
            div_tete.append(span_info);
            div_tete.append(supp);
            div_tete.append(ajouter);
            div.append(div_tete); 
            div.append(corps);
            $("#div_Superfamille_"+sup_fam_id).children(" .div_corps").append(div);
        }
        derouler("div_Superfamille_"+sup_fam_id);
    }
    else {
        enrouler("div_Superfamille_"+sup_fam_id);
    }
}

function getSousFamilleList(sup_fam_id,fam_id){
    
    if(verification("div_Famille_"+fam_id)){
        var id_sp = retreive_Id(sup_fam_id);
        var id_fa = retreive_Id(fam_id);
        var data = "nav=sousFamille&supFam="+ id_sp+"&fam="+ id_fa;
        var listeSousFamille = ajaxSend(data);

        for(var i=0; i<listeSousFamille.length; i++){
            var id=  get_Id_Unique(listeSousFamille[i]['id']);
            var div = $("<div id='div_Sousfamille_" + id + "' class='div_Sousfamille' style='display:block'></div>");
            var div_tete = $("<div class='div_entete'></div>");
            var span_info = $("<span class='div_title'>Sousfamille : </span>");
            var span = $("<span>" + listeSousFamille[i]['nom'] + "</span>");
            var ajouter ="<a class='lien_ajout' onclick=\"ajouter('tribu', "+listeSousFamille[i]['id']+");\" ><span>Ajouter tribu(s)</span></a>"
            var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + listeSousFamille[i]['id'] + ", '" + listeSousFamille[i]['nom'] + "');\" />");
            var corps = $("<div class='div_corps' style='display:none;'></div>");
            var fleche = $("<img src='../../content/images/fleche_bas.png' id='fl_"+id+"' class='img_fleche img' onclick=\"getTribuList('"+sup_fam_id+"', '"+fam_id+"', '" + id + "');\" />");
            div_tete.append(fleche);
            div_tete.append(span);
            div_tete.append(span_info);
            div_tete.append(supp);
            div_tete.append(ajouter);
            div.append(div_tete); 
            div.append(corps);
            $("#div_Famille_"+fam_id).children(" .div_corps").append(div);
        }
        derouler("div_Famille_"+fam_id);
    }
    else {
        enrouler("div_Famille_"+fam_id);
    }
}

function getTribuList(sup_fam_id,fam_id,sous_fam_id){
    
    if(verification("div_Sousfamille_"+sup_fam_id)){
        var id_sp = retreive_Id(sup_fam_id);
        var id_fa = retreive_Id(fam_id);
        var id_sf = retreive_Id(sous_fam_id);
        var data = "nav=tribu&supFam="+ id_sp+"&fam="+ id_fa+"&sousFam="+ id_sf;
        var listeTribu = ajaxSend(data);

        for(var i=0; i<listeTribu.length; i++){
            var id= get_Id_Unique(listeTribu[i]['id']);
            var div = $("<div id='div_Tribu_" + id + "' class='div_Tribu' style='display:block'></div>");
            var div_tete = $("<div class='div_entete'></div>");
            var span_info = $("<span class='div_title'>Tribu : </span>");
            var span = $("<span>" + listeTribu[i]['nom'] + "</span>");
            var ajouter ="<a class='lien_ajout' onclick=\"ajouter('tribu', "+listeTribu[i]['id']+");\" ><span>Ajouter Genre(s)</span></a>"
            var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + listeTribu[i]['id'] + ", '" + listeTribu[i]['nom'] + "');\" />");
            var corps = $("<div class='div_corps" + "' style='display:none;'></div>");
            var fleche = $("<img src='../../content/images/fleche_bas.png' class='img_fleche img' id='fl_"+id+"' onclick=\"getGenreList('"+sup_fam_id+"', '"+fam_id+"', '"+sous_fam_id+"', '" + id + "');\" />");
            div_tete.append(fleche);
            div_tete.append(span);
            div_tete.append(span_info);
            div_tete.append(supp);
            div_tete.append(ajouter);
            div.append(div_tete); 
            div.append(corps);
            $("#div_Sousfamille_"+sous_fam_id).children(" .div_corps").append(div);
        }
        derouler("div_Sousfamille_"+sous_fam_id);
    }
    else {
        enrouler("div_Sousfamille_"+sous_fam_id);
    }
}

function getGenreList(sup_fam_id,fam_id,sous_fam_id,tribu_id){
    
    if(verification("div_Tribu_"+sup_fam_id)){
        var id_sp = retreive_Id(sup_fam_id);
        var id_fa = retreive_Id(fam_id);
        var id_sf = retreive_Id(sous_fam_id);
        var id_tr = retreive_Id(tribu_id);
        var data = "nav=genre&supFam="+ id_sp+"&fam="+ id_fa+"&sousFam="+ id_sf+"&tribu="+ id_tr;
        var listeGenre = ajaxSend(data);

        for(var i=0; i<listeGenre.length; i++){
            var id=  get_Id_Unique(listeGenre[i]['id']);
            var div = $("<div id='div_Genre_" + id + "' class='div_Genre' style='display:block'></div>");
            var div_tete = $("<div class='div_entete'></div>");
            var span_info = $("<span class='div_title'>Genre : </span>");
            var span = $("<span>" + listeGenre[i]['nom'] + "</span>");
            var ajouter ="<a class='lien_ajout' onclick=\"ajouter('genre', "+listeGenre[i]['id']+");\" ><span>Ajouter Sous-genre(s)</span></a>"
            var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + listeGenre[i]['id'] + ", '" + listeGenre[i]['nom'] + "');\" />");
            var corps = $("<div class='div_corps" + "' style='display:none;'></div>");
            var fleche = $("<img src='../../content/images/fleche_bas.png' class='img_fleche img' id='fl_"+id+"' onclick=\"getSousGenreList('"+sup_fam_id+"', '"+fam_id+"', '"+sous_fam_id+"', '"+tribu_id+"', '" + id + "');\" />");
            div_tete.append(fleche);
            div_tete.append(span);
            div_tete.append(span_info);
            div_tete.append(supp);
            div_tete.append(ajouter);
            div.append(div_tete); 
            div.append(corps);
            $("#div_Tribu_"+tribu_id).children(" .div_corps").append(div);
        }
        derouler("div_Tribu_"+tribu_id);
    }
    else {
        enrouler("div_Tribu_"+tribu_id);
    }
}

function getSousGenreList(sup_fam_id,fam_id,sous_fam_id,tribu_id,genre_id){
    
    if(verification("div_Genre_"+sup_fam_id)){
        var id_sp = retreive_Id(sup_fam_id);
        var id_fa = retreive_Id(fam_id);
        var id_sf = retreive_Id(sous_fam_id);
        var id_tr = retreive_Id(tribu_id);
        var id_ge = retreive_Id(genre_id);
        var data = "nav=sousGenre&supFam="+ id_sp+"&fam="+ id_fa+"&sousFam="+ id_sf+"&tribu="+ id_tr+"&genre="+ id_ge;
        var listeSousGenre = ajaxSend(data);

        for(var i=0; i<listeSousGenre.length; i++){
            var id=  get_Id_Unique(listeSousGenre[i]['id']);
            var div = $("<div id='div_SousGenre_" + id + "' class='div_SousGenre' style='display:block'></div>");
            var div_tete = $("<div class='div_entete'></div>");
            var span_info = $("<span class='div_title'>Genre : </span>");
            var span = $("<span>" + listeSousGenre[i]['nom'] + "</span>");
            var ajouter ="<a class='lien_ajout' onclick=\"ajouter('sousGenre', "+listeSousGenre[i]['id']+");\" ><span>Ajouter Espèce(s)</span></a>"
            var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + listeSousGenre[i]['id'] + ", '" + listeSousGenre[i]['nom'] + "');\" />");
            var corps = $("<div class='div_corps" + "' style='display:none;'></div>");
            var fleche = $("<img src='../../content/images/fleche_bas.png' class='img_fleche img' id='fl_"+id+"' onclick=\"getEspeceList('"+sup_fam_id+"', '"+fam_id+"', '"+sous_fam_id+"', '"+tribu_id+"', '"+genre_id+"', '" + id + "');\" />");
            div_tete.append(fleche);
            div_tete.append(span);
            div_tete.append(span_info);
            div_tete.append(supp);
            div_tete.append(ajouter);
            div.append(div_tete); 
            div.append(corps);
            $("#div_Genre_"+genre_id).children(" .div_corps").append(div);
        }
        derouler("div_Genre_"+genre_id);
    }
    else {
        enrouler("div_Genre_"+genre_id);
    }
}

//-------------------------------EVENEMENTS--------------------------
//derouler(div_sup_fam_" + listeSuperFamille[i]['id']+ ");
function derouler(id_div){
    $('#'+id_div).children(".div_corps").show('top');
    $('#'+id_div).children(".div_entete").children(".img_fleche").attr('src', '../../content/images/fleche_gauche.png');
    //--Change le click
    document.getElementById($('#'+id_div).children(".div_entete").children(".img_fleche").attr('id')).setAttribute('onclick', "enrouler('"+id_div+"');");
}

function enrouler(id_div){
    
    //$('#'+id_div /*+ ' .'+classe_apparition*/ + ".div_corps").hide('bottom', function(){
    $('#'+id_div).children(".div_corps").hide('bottom');
    $('#'+id_div).children(".div_entete").children(".img_fleche").attr('src', '../../content/images/fleche_bas.png');
    //--Change le click
    document.getElementById($('#'+id_div).children(".div_entete").children(".img_fleche").attr('id')).setAttribute('onclick', "derouler('"+id_div+"');");
}

function verification(id_div){
    var val = $('#'+id_div+" > .div_entete > .img_fleche").attr('src');
    //-Si la div est déja déroulée
    if(val == '../../content/images/fleche_gauche.png'){
        return false;
    }
    else return true;
}

function suppression(id, nom){
    alert('Suppression à effectuer');
}

//--Gestion des id

var id_unique = 0;
function get_Id_Unique(prefix){return prefix+"_"+id_unique++;};
function retreive_Id(string){
    return string.split("_")[0];
}
//-------------------------------------------------------------------
//-------------------------------Fonctions privées--------------------------
//--A penser
function constructionItem(liste, nom, div_a_completer, id_a_completer, fonctionSuivante, premiere){
    
    for(var i=0; i<liste.length; i++){
        var div;
        if(premiere){
            div = $("<div id='div_"+nom+"_" + liste[i]['id'] + "' class='div_"+nom+"' style='display:block'></div>");
        }
        else{
            div = $("<div id='div_"+nom+"_" + liste[i]['id'] + "' class='div_"+nom+"' style='display:none'></div>");
        }
        var div_tete = $("<div class='div_entete'><span class='div_title'>"+nom+" : </span></div>");
        var span = $("<span>" + liste[i]['nom'] + "</span>");
        var supp = $("<img src='../../content/images/nok.png' class='img_supp img' onclick=\"suppression(" + liste[i]['id'] + ", '" + liste[i]['nom'] + "');\" />");
        var fleche = $("<img src='../../content/images/fleche_bas.png' class='img_fleche img' id='fleche_"+nom+"_" + liste[i]['id'] + "' onclick='"+fonctionSuivante+"(" + liste[i]['id'] + ");' />");
        div_tete.append(span);
        div_tete.append(fleche);
        div_tete.append(supp);
        div.append(div_tete);
        $("#"+div_a_completer+id_a_completer).append(div);
    }
    if(!premiere){
        derouler(div_a_completer+id_a_completer, 'div_'+nom);
    }
}

function ajaxSend(data){
    var donnee;
    $.ajax({
        type: 'POST',
        url: 'admin_taxon.behind.php',
        data: data,
        async: false,
        success: function(data) {
            donnee = eval('('+ data +')');
        }
    });
    return donnee;
}

function labelModification(text, iserror){
    var lab = $('#label_error');
    if(iserror){
       lab.attr('style', 'color:red;') ;
    }
    else{
       lab.attr('style', 'color:green;') ;
    }
    lab.text(text);
    lab.fadeIn(3000, function(){
        $(this).fadeOut(3000)
    });
}
//-------------------------------------------------------------------
 